/*
 *    Copyright (c) 2018-2025, boss All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer.
 * Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * Neither the name of the boss developer nor the names of its
 * contributors may be used to endorse or promote products derived from
 * this software without specific prior written permission.
 * Author: boss
 */
package com.hopes.bizz.close.basic.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.hopes.bizz.close.basic.entity.ClosedGpsDeviceEntity;
import com.hopes.bizz.close.basic.dto.ClosedGpsDeviceExcelDTO;
import com.hopes.bizz.close.basic.vo.ClosedGpsDeviceExcelVO;
import com.hopes.bizz.close.basic.mapper.ClosedGpsDeviceMapper;
import com.hopes.bizz.close.basic.service.ClosedGpsDeviceService;
import com.hopes.bizz.close.common.constant.BasicDicConstant;
import com.hopes.bizz.close.common.constant.CacheConstants;
import com.hopes.bizz.close.common.constant.enums.ParkCloseSysDictEnum;
import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.common.core.validate.ValidatorUtils;
import com.hopes.boss.admin.api.resolver.DictResolver;
import com.hopes.boss.admin.api.resolver.enums.SysDictEnum;
import com.hopes.boss.common.core.constant.CommonConstants;
import com.hopes.boss.common.entity.ValidateImportBO;
import com.hopes.boss.common.excel.vo.ErrorMessage;
import org.springframework.beans.BeanUtils;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
/**
 * GPS设备
 *
 * @author pig
 * @date 2023-10-24 17:17:29
 */
@Service
public class ClosedGpsDeviceServiceImpl extends ServiceImpl<ClosedGpsDeviceMapper, ClosedGpsDeviceEntity> implements ClosedGpsDeviceService {


    @Override
    public boolean save(ClosedGpsDeviceEntity entity) {
        entity.setBindFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        entity.setDeviceStatus(BasicDicConstant.CLOSE_GPS_DEVICE_STATUS_NORMAL);
        entity.setNetworkStatus(BasicDicConstant.CLOSE_DEVICE_NETWORK_STATUS_NORMAL);
        return SqlHelper.retBool(getBaseMapper().insert(entity));
    }

    @Override
    @Cacheable(value = CacheConstants.CLOSE_GPS_DEVICE + "#60", unless = "#result.isEmpty()")
    public List<ClosedGpsDeviceEntity> queryGpsDeviceList() {
        return getBaseMapper().selectList(Wrappers.lambdaQuery());
    }

    @Override
    public ValidateImportBO<ClosedGpsDeviceExcelDTO> validateImport(List<ClosedGpsDeviceExcelDTO> excelDTOList) {
        ValidateImportBO<ClosedGpsDeviceExcelDTO> bo = new ValidateImportBO<>();
        List<ErrorMessage> errorMessageList = new ArrayList<>();
        List<ClosedGpsDeviceExcelDTO> saveList = new ArrayList<>();
        int successNum = 0;
        int failedNum = 0;

        for (ClosedGpsDeviceExcelDTO closedGpsDeviceExcelDTO : excelDTOList) {
            final String validateRes = ValidatorUtils.importValidateEntity(closedGpsDeviceExcelDTO);
            if (StrUtil.isEmpty(validateRes)) {
                successNum++;
                saveList.add(closedGpsDeviceExcelDTO);
            } else {
                failedNum++;
                errorMessageList.add(new ErrorMessage(closedGpsDeviceExcelDTO.getLineNum(), new HashSet<>(){
                    private static final long serialVersionUID = 2114050778826325599L;
                    {
                        add(validateRes);
                    }
                }));
            }
        }

        bo.setErrorMessages(errorMessageList);
        bo.setSaveList(saveList);
        bo.setSuccessNum(successNum);
        bo.setFailedNum(failedNum);
        return bo;
    }

    @Override
    public ClosedGpsDeviceEntity importDtoToEntity(ClosedGpsDeviceExcelDTO excelDto) {
        ClosedGpsDeviceEntity entity= new ClosedGpsDeviceEntity();
        BeanUtils.copyProperties(excelDto, entity);
        if (StrUtil.isNotEmpty(excelDto.getUseFlagStr())) {
            entity.setUseFlag(DictResolver.getItemValueByLabel(SysDictEnum.yes_no_type, excelDto.getUseFlagStr()));
        }
        entity.setBindFlag(CommonConstants.DEFAULT_FALSE_FLAG);
        entity.setDeviceStatus(BasicDicConstant.CLOSE_GPS_DEVICE_STATUS_NORMAL);
        entity.setNetworkStatus(BasicDicConstant.CLOSE_DEVICE_NETWORK_STATUS_NORMAL);
        return entity;
    }

    @Override
    public ClosedGpsDeviceExcelVO exportEntityToVo(ClosedGpsDeviceEntity entity) {
        ClosedGpsDeviceExcelVO excelVO = new ClosedGpsDeviceExcelVO();
        BeanUtils.copyProperties(entity, excelVO);
        excelVO.setDataTimeStr(DateUtils.limitSecond(entity.getDataTime()));
        excelVO.setUseFlagStr(DictResolver.getItemLabelByValue(SysDictEnum.yes_no_type, entity.getUseFlag()));
        excelVO.setBindFlagStr(DictResolver.getItemLabelByValue(SysDictEnum.yes_no_type, entity.getBindFlag()));
        excelVO.setDeviceStatusStr(DictResolver.getItemLabelByValue(ParkCloseSysDictEnum.close_gps_device_status, entity.getDeviceStatus()));
        excelVO.setNetworkStatusStr(DictResolver.getItemLabelByValue(ParkCloseSysDictEnum.close_device_network_status, entity.getNetworkStatus()));
        return excelVO;
    }


}
